package se.mbaeumer.tinydbc.core.dbclient;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class MySQLDBClient extends AbstractDBClient {

	public MySQLDBClient(String databaseName, String username, String password) {
		this.databaseName = databaseName;
		this.username = username;
		this.password = password;
	}

	@Override
	public void connect() {
		// TODO Auto-generated method stub
		try {
			if (connection == null) {
				connection = DriverManager
						.getConnection("jdbc:mysql://localhost/?user="
								+ this.username + "&password=" + this.password);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void createDatabase(String databaseName) {
		// TODO Auto-generated method stub
		System.out.println(this.getDatabaseName() + ":"
				+ "create MySQL database");

		this.connect();
		try {
			Statement s = connection.createStatement();
			int result = s.executeUpdate("CREATE DATABASE " + databaseName
					+ ";");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void createUser(String username, String password) {
		String query = "CREATE USER " + username;
		String identifiedBy = null;
		if (password != null){
			identifiedBy = "IDENTIFIED BY " + password;
		}
		query = query + identifiedBy + ";";
		this.connect();
		try {
			Statement s = connection.createStatement();
			int result = s.executeUpdate(query);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public List<String> showUsers() {
		// TODO Auto-generated method stub
		// check if there is a connection
		List<String> userNames = new ArrayList<String>();

		this.connect();

		try {
			Statement s = connection.createStatement();
			ResultSet users = s.executeQuery("SELECT User from mysql.user;");
			while (users.next()) {
				userNames.add(users.getString("User"));
				System.out.println(users.getString("User"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return userNames;
	}

	@Override
	public List<String> showDatabases() {
		// TODO Auto-generated method stub
		// check if there is a connection
		List<String> databaseNames = new ArrayList<String>();

		this.connect();

		try {
			Statement s = connection.createStatement();
			ResultSet databases = s.executeQuery("SHOW DATABASES;");
			while (databases.next()) {
				databaseNames.add(databases.getString("Database"));
				System.out.println(databases.getString("Database"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return databaseNames;
	}

	public void disconnect() {
		try {
			connection.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void grantRights() {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean existsUser(String username) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean existsDatabase(String databaseName) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void switchDatabase() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void switchUser() {
		// TODO Auto-generated method stub
		
	}

}
